package cn.nexd.location.thread;

import cn.nexd.location.NexdLocation;
import cn.nexd.location.NexdLocationListener;
import cn.nexd.location.collector.NexdCollector;
import cn.nexd.location.utils.threadpool.ThreadTaskObject;
import cn.nexd.location.utils.threadpool.core.ThreadPoolDelegate;

/**
 * Created by codngfish on 15/12/21.
 */
public class HandlerLocationEventThread {
    private NexdLocationListener locationListener;
    private ThreadTaskObject handlerLocation = null;

    public HandlerLocationEventThread(final NexdLocationListener locationListener) {
        this.locationListener = locationListener;
    }

    public void handler(int type, String message, int collectorType) {
        if (locationListener != null) {
            handlerLocation = new HandlerLocation(type, message, collectorType);
            ThreadPoolDelegate.getThreadPoolDelegate().getThreadPoolManager().addTask(handlerLocation);
        }
    }

    public void stop() {
        if (handlerLocation != null) {
            ThreadPoolDelegate.getThreadPoolDelegate().getThreadPoolManager().removeTask(handlerLocation);
            handlerLocation = null;
        }
    }

    private class HandlerLocation extends ThreadTaskObject {

        private int type;
        private String message;
        private int collectorType;

        public HandlerLocation(int type, String message, int collectorType) {
            this.type = type;
            this.message = message;
            this.collectorType = collectorType;
        }

        @Override
        public void run() {
            switch (type) {
                //////////////////// stop
                case NexdCollector.COLLECTOR_TASK_STOP_TYPE_COLLECTOR_STATE_UNAVAILABLE:
                    //采集器当前状态为不可用
                    if (collectorType == 0) {
                        //wifi
                        locationListener.onLocationInterrupt(NexdLocation.LOCATION_INTERRUPT_TYPE_WIFI_UNAVAILABLE, NexdLocation.LOCATION_INTERRUPT_MESSAGE_WIFI_UNAVAILABLE);
                    } else if (collectorType == 1) {
                        // bluetooth
                        locationListener.onLocationInterrupt(NexdLocation.LOCATION_INTERRUPT_TYPE_BLUETOOTH_UNAVAILABLE, NexdLocation.LOCATION_INTERRUPT_MESSAGE_BLUETOOTH_UNAVAILABLE);
                    }
                    break;
                case NexdCollector.COLLECTOR_TASK_STOP_TYPE_PERMISSION_UNAVAILABLE:
                    //请检查权限设置
                    if (locationListener != null) {
                        locationListener.onLocationInterrupt(NexdLocation.LOCATION_INTERRUPT_TYPE_PERMISSION_UNAVAILABLE, NexdLocation.LOCATION_INTERRUPT_MESSAGE_PERMISSION_UNAVAILABLE);
                    }
                    break;
                case NexdCollector.COLLECTOR_TASK_STOP_TYPE_COLLECTOR_UNAVAILABLE:
                    //不支持该采集器类型
                    if (locationListener != null) {
                        locationListener.onLocationInterrupt(NexdLocation.LOCATION_INTERRUPT_TYPE_COLLECTOR_UNAVAILABLE, NexdLocation.LOCATION_INTERRUPT_MESSAGE_COLLECTOR_UNAVAILABLE);
                    }
                    break;

                //////////////////// failed
                case NexdCollector.COLLECTOR_TASK_FAILED_TYPE_COLLECTOR_STATE_OPENNING:
                    //采集器正在开启
                case NexdCollector.COLLECTOR_TASK_FAILED_TYPE_RESULT_NULL:
                    //采集结果为空
                    if (locationListener != null) {
                        locationListener.onLocationFailed(NexdLocation.LOCATION_FAILED_TYPE_RESULT_NULL, NexdLocation.LOCATION_FAILED_MESSAGE_RESULT_NULL);
                    }
                    break;
                case NexdLocation.LOCATION_FAILED_TYPE_DATE_TOO_LESS:
                    if (locationListener != null) {
                        locationListener.onLocationFailed(NexdLocation.LOCATION_FAILED_TYPE_DATE_TOO_LESS, NexdLocation.LOCATION_FAILED_MESSAGE_DATE_TOO_LESS);
                    }
                    break;
                case NexdCollector.COLLECTOR_TASK_STOP_TYPE_UNNATURE_ACCESS_DATA:
                    if (locationListener != null) {
                        locationListener.onLocationInterrupt(NexdLocation.LOCATION_INTERRUPT_TYPE_UNNATURE_ACCESS_DATA, NexdLocation.LOCATION_INTERRUPT_MESSAGE_UNNATURE_ACCESS_DATA);
                    }
                    break;
            }
        }
    }
}

